﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace EstevesRecordersProject
{
    /// <summary>
    /// Interaction logic for WindowsLoadedDate.xaml
    /// </summary>
    public partial class WindowsLoadedDate : Window
    {

        Recorder recorder;

        private void ClearLabels()
        {
            lblMessage.Content = String.Empty;
            lblPercent.Content = String.Empty;
            lblResult.Content = String.Empty;
        }

        public WindowsLoadedDate()
        {
            InitializeComponent();
            ClearLabels();
        }

        private void InitializeComboBoxRecorders()
        {
            List<Recorder> recorders = Repository.GetAllRecorders();
            comboBoxRecorders.ItemsSource = recorders;
            comboBoxRecorders.SelectedValuePath = "ID";
        }

        public void InitializeCalendar(DateTime from, DateTime to)
        {
            try
            {
                if (recorder == null) throw new NoRecorderSelectedException();
                calendar.BlackoutDates.Clear();

                List<DateTime> days = Repository.GetLoadedDays(recorder, from, to);

                //zaznacz wczytane dni
                foreach (DateTime d in days)
                {
                    if (days.Count > 0) calendar.BlackoutDates.Add(new CalendarDateRange(d, d));
                }
                
                lblPercent.Content = days.Count + "/" + ((to - from).Days + 1);

            }
            catch (NoRecorderSelectedException ex)
            {
                lblMessage.Foreground = Brushes.Red;
                lblMessage.Content = ex.Message;
            }
            catch (Exception ex)
            {
                lblMessage.Foreground = Brushes.Red;
                lblMessage.Content = "No file was loaded for that month.";
            }
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                InitializeComboBoxRecorders();
                ClearLabels();
            }
            catch (Exception ex)
            {
                lblMessage.Content = ex.Message;
                lblMessage.Foreground = Brushes.Red;
            }
        }


        private void comboBoxRecorders_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                ClearLabels();
                recorder = (Recorder)comboBoxRecorders.SelectedItem;

                DateTime? d1 = new DateTime(calendar.DisplayDate.Year, calendar.DisplayDate.Month, 1);
                DateTime? d2 =new DateTime(calendar.DisplayDate.Year, calendar.DisplayDate.Month, 1).AddMonths(1);

                InitializeCalendar(d1.Value, d2.Value);
            }
            catch (Exception ex)
            {
                lblMessage.Content = ex.Message;
                lblMessage.Foreground = Brushes.Red;
            }
        }

        private void calendar_DisplayDateChanged(object sender, CalendarDateChangedEventArgs e)
        {
            ClearLabels();
            DateTime? loadedData = e.AddedDate;
            InitializeCalendar(loadedData.Value, loadedData.Value.AddMonths(1));       
        }

        private void expander_Expanded(object sender, RoutedEventArgs e)
        {
            this.Height += expander.Height - 20;
        }

        private void expander_Collapsed(object sender, RoutedEventArgs e)
        {
            this.Height -= expander.Height - 20;
        }

        private void btnCount_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ClearLabels();
                if (recorder == null) throw new NoRecorderSelectedException(); 
                DateTime? from = datePicker2.SelectedDate;
                DateTime? to = datePicker1.SelectedDate;

                if (from == null) throw new ApplicationException("Start time have to be choosen.");
                if (to == null) throw new ApplicationException("End time have to be choosen.");
                if (from.Value > to.Value) throw new ApplicationException("Start time have to be lower than end time.");

                List<DateTime> days =  Repository.GetLoadedDays(recorder, from.Value, to.Value);

                lblResult.Content = days.Count + "/" + ((to.Value - from.Value).Days + 1) + " (" + (days.Count*100)/((to.Value - from.Value).Days + 1) + "%)";
            }
            catch (Exception ex)
            {
                lblMessage.Content = ex.Message;
                lblMessage.Foreground = Brushes.Red;
            }
        }


    }
}
